/*
 * This is  a part of the Video Resource System(VRS).
 * Copyright (C) 2010-2012 iqiyi.com Corporation
 * All rights reserved.
 *
 * Licensed under the iqiyi.com private License.
 */
package com.iqiyi.pps.epg.core.utils;

import org.apache.commons.lang3.StringUtils;

/**
 * @author wangweiping
 * 
 */
public final class UrlGeneratorUtil {
	private static final String BASE62 = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	private static final String SEED = "lgqipu";
    private static final String BASEURL = "http://www.iqiyi.com/";

	private static long l_seed = toLong(SEED);

	public static String encodeBase36(long num) {
		return encode(num, 36);
	}

	public static String str2hex(String seed) {
		StringBuilder result = new StringBuilder();
		for (int i = seed.length() - 1; i >= 0; i--) {
			int c = seed.charAt(i);
			result.append(Integer.toHexString(c));
		}
		return result.toString();
	}

	public static long toLong(String seed) {
		long v = Long.parseLong(str2hex(seed), 16);
		return v;
	}

	public static long decodeBase36(String str) {
		return decode(str, 36);
	}

	public static String encode(long num, int radix) {
		if (num < 0) {
			throw new IllegalArgumentException("num must be positive");
		}
		if (radix < 2 || radix > 62) {
			throw new IllegalArgumentException("radix must be between 2 and 62");
		}
		long q = num;
		int r = 0;
		StringBuilder result = new StringBuilder();
		while (q != 0) {
			q = num / radix;
			r = (int) (num % radix);
			result.append(BASE62.charAt(r));
			num = q;
		}
		return result.reverse().toString();
	}

	public static long decode(String str, int radix) {
		char[] chars = str.toCharArray();
		long result = 0;
		for (int i = 0; i < chars.length; i++) {
			int digit = BASE62.indexOf(chars[i]);
			result += digit * Math.pow(radix, chars.length - (i + 1));
		}
		return result;
	}

	public static long decodeUrl(String shortUrl) {
		long id = decodeBase36(shortUrl);
		return id ^ l_seed;
	}

	public static String generateUrl(long id) {
		long xorValue = id ^ l_seed;
		String base36Value = encodeBase36(xorValue);
		if (base36Value == null) {
			return null;
		}
		if (base36Value.length() >= 4) {
			return base36Value;
		}
		StringBuilder result = new StringBuilder(4);
		for (int i = 0; i < (4 - base36Value.length()); i++) {
			result.append("0");
		}
		result.append(base36Value);
		return result.toString();
	}

    public static String generateUgcUrl(long id){
        String generateStr = generateUrl(id);
        StringBuilder sb = new StringBuilder();
        sb.append(BASEURL).append("w_").append(generateStr).append(".html");
        return sb.toString();
    }

	public static long ip2Long(String ip) throws Exception {
		String[] splits = ip.split("\\.");
		long a, b, c, d;

		a = Long.parseLong(splits[0]);
		b = Long.parseLong(splits[1]);
		c = Long.parseLong(splits[2]);
		d = Long.parseLong(splits[3]);

		return (a << 24) + (b << 16) + (c << 8) + d;
	}

	public static int byte2int(byte b) {
		int l = b & 0x07f;
		if (b < 0) {
			l |= 0x80;
		}
		return l;
	}

	public static String long2ip(int ip) {
		int[] b = new int[4];
		b[0] = (int) ((ip >> 24) & 0xff);
		b[1] = (int) ((ip >> 16) & 0xff);
		b[2] = (int) ((ip >> 8) & 0xff);
		b[3] = (int) (ip & 0xff);
		String x;
		Integer p;
		p = new Integer(0);
		x = p.toString(b[0]) + "." + p.toString(b[1]) + "." + p.toString(b[2]) + "." + p.toString(b[3]);

		return x;

	}

    public static String getChannelPageUrl(long qipuId , int type){
        String pageUrl = generateUrl(qipuId);
        switch(type){
            case 1:
            case 2:
            case 3:
                return  String.format("http://www.iqiyi.com/c_%s.html",pageUrl);
            default:
                return null;
        }

    }

	public static void main(String[] args) throws Exception {
//		String url="http://www.iqiyi.com/v_19rrifvu2a.html";
//			String shortUrlName = StringUtils.substringBetween(url, "/v_", ".html");
//			System.out.println(decodeBase36(shortUrlName) ^ l_seed);
			//tvIds.add((decodeBase36(shortUrlName) ^ l_seed) + "");
			// System.out.println(decodeBase36(shortUrlName)^l_seed);

		//FileUtils.writeLines(new File("d:/episodeIds.txt"), tvIds);
        String.format("http://www.iqiyi.com/l_%s","19rrhcznej");
        System.out.println(String.format("http://www.iqiyi.com/l_%s","19rrhcznej"));
	}
}
